package daily.may0517;

import java.util.Arrays;

public class ArraysTest {
    //输出数组专用方法
    public static void showArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }

    public static void main(String[] args) {
        int[] array = new int[]{4, 16, 9, 1, 49, 81, 64, 25, 36};

        //copyOfRange方法，截取下标1-4的元素拷贝到新数组
        int[] arrayIntercept = Arrays.copyOfRange(array, 1, 5);
        showArray(arrayIntercept);//16 9 1 49

        //sort方法，排序，默认升序
        Arrays.sort(array);
        showArray(array);//1 4 9 16 25 36 49 64 81

        //binarySearch方法，查找元素的下标，找不到返回负数
        int index = Arrays.binarySearch(array, 49);
        System.out.println("元素49的下标：" + index);//6
        index = Arrays.binarySearch(array,999);
        System.out.println("元素999的下标：" + index);//-10

        //toString方法，将数组转换为字符串
        String s = Arrays.toString(array);
        System.out.println(s);//[1, 4, 9, 16, 25, 36, 49, 64, 81]

        //fill方法，将999填充到数组的下标2-4
        Arrays.fill(array,2,5,999);
        showArray(array);//1 4 999 999 999 36 49 64 81
    }
}
